package com.reservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.common.Result;
import com.reservation.dto.ExhibitionCollectionDTO;
import com.reservation.entity.ArtCollection;
import com.reservation.entity.Exhibition;
import com.reservation.mapper.CollectionMapper;
import com.reservation.mapper.ExhibitionMapper;
import com.reservation.service.CollectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 藏品控制器
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private ExhibitionMapper exhibitionMapper;
    @Autowired
    private CollectionService collectionService;

    /**
     * 添加藏品（仅管理员可用）
     *
     * @param artCollection 藏品信息
     * @return 添加结果
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<ArtCollection> addCollection(@RequestBody @Valid ArtCollection artCollection) {
        ArtCollection addedArtCollection = collectionService.addCollection(artCollection);
        return Result.success(addedArtCollection, "添加成功");
    }

    /**
     * 修改藏品信息（仅管理员可用）
     *
     * @param artCollection 藏品信息
     * @return 修改结果
     */
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<ArtCollection> updateCollection(@RequestBody @Valid ArtCollection artCollection) {
        ArtCollection updatedArtCollection = collectionService.updateCollection(artCollection);
        return Result.success(updatedArtCollection, "修改成功");
    }

    /**
     * 获取藏品详情
     *
     * @param id 藏品ID
     * @return 藏品信息
     */
    @GetMapping("/{id}")
    public Result<ArtCollection> getCollectionById(@PathVariable Long id) {
        ArtCollection artCollection = collectionService.getCollectionById(id);
        return Result.success(artCollection);
    }

    /**
     * 删除藏品（仅管理员可用）
     *
     * @param id 藏品ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteCollection(@PathVariable Long id) {
        boolean result = collectionService.deleteCollection(id);
        return Result.success(null, "删除成功");
    }

    /**
     * 分页查询藏品列表
     *
     * @param pageNum      当前页
     * @param pageSize         每页大小
     * @param name         藏品名称（可选）
     * @param type         藏品类型（可选）
     * @param exhibitionId 藏品年代（可选）
     * @return 藏品列表
     */
    @GetMapping("/list")
    public Result<IPage<ExhibitionCollectionDTO>> getCollectionList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Long exhibitionId) {
        Page<ExhibitionCollectionDTO> page = new Page<>(pageNum, pageSize);
        IPage<ExhibitionCollectionDTO> exhibitionWithCollections = collectionMapper.getExhibitionWithCollections(page, exhibitionId, name);
        return Result.success(exhibitionWithCollections);
    }

    /**
     * 获取所有藏品
     *
     * @return 藏品列表
     */
    @GetMapping("/all")
    public Result<List<ArtCollection>> getAllCollections() {
        List<ArtCollection> artCollections = collectionService.getAllCollections();
        return Result.success(artCollections);
    }

    /**
     * 根据类型获取藏品
     *
     * @param type 藏品类型
     * @return 藏品列表
     */
    @GetMapping("/type/{type}")
    public Result<List<ArtCollection>> getCollectionsByType(@PathVariable String type) {
        List<ArtCollection> artCollections = collectionService.getCollectionsByType(type);
        return Result.success(artCollections);
    }

    /**
     * 根据年代获取藏品
     *
     * @param era 藏品年代
     * @return 藏品列表
     */
    @GetMapping("/era/{era}")
    public Result<List<ArtCollection>> getCollectionsByEra(@PathVariable String era) {
        List<ArtCollection> artCollections = collectionService.getCollectionsByEra(era);
        return Result.success(artCollections);
    }

    /**
     * 获取展览中的藏品
     *
     * @param exhibitionId 展览ID
     * @return 藏品列表
     */
    @GetMapping("/exhibition/{exhibitionId}")
    public Result<List<ArtCollection>> getCollectionsByExhibition(@PathVariable Long exhibitionId) {
        List<ArtCollection> artCollections = collectionService.getCollectionsByExhibition(exhibitionId);
        return Result.success(artCollections);
    }

    /**
     * 添加藏品到展览（仅管理员可用）
     *
     * @param exhibitionId 展览ID
     * @param collectionId 藏品ID
     * @return 添加结果
     */
    @PostMapping("/exhibition/{exhibitionId}/collection/{collectionId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> addCollectionToExhibition(
            @PathVariable Long exhibitionId,
            @PathVariable Long collectionId) {
        boolean result = collectionService.addCollectionToExhibition(exhibitionId, collectionId);
        return Result.success(null, "添加成功");
    }

    /**
     * 从展览中移除藏品（仅管理员可用）
     *
     * @param exhibitionId 展览ID
     * @param collectionId 藏品ID
     * @return 移除结果
     */
    @DeleteMapping("/exhibition/{exhibitionId}/collection/{collectionId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> removeCollectionFromExhibition(
            @PathVariable Long exhibitionId,
            @PathVariable Long collectionId) {
        boolean result = collectionService.removeCollectionFromExhibition(exhibitionId, collectionId);
        return Result.success(null, "移除成功");
    }
}